#include "playground.h"
#include "buttonitem.h"
#include "canvas.h"
#include "level.h"
#include "graphjudger.h"

Playground::Playground(QGraphicsItem *parent, QGraphicsScene *scene) :
    QGraphicsItem(parent, scene)
{
    initScene();
    initEventSystem();
}


void Playground::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{

}

QRectF Playground::boundingRect() const
{
    return scene()->sceneRect();
}

void Playground::initScene()
{
    background = new ImageItem(":/playground", QPoint(),
                               QPoint(ImageItem::BG_SIZE.width(),
                                      ImageItem::BG_SIZE.height()),
                               scene(), NULL);

    canvas = new Canvas(QString(), QPoint(122, 80), QPoint(694, 486), scene(), background);

    returnBg = new ButtonItem(":/editor-return", QPoint(69, 477),
                             QPoint(116, 521), scene(), background);

    greatImg = new ImageItem(":/great", QPoint(186, 134), QPoint(614, 420),
                             scene(), background);
    greatImg->setVisible(false);

    judger = new GraphJudger();
    timer = new QTimer();

}

void Playground::initEventSystem()
{
    connect(returnBg, SIGNAL(clicked(ButtonItem*)), this, SLOT(clicked(ButtonItem*)));
    connect(canvas, SIGNAL(lineHoverEnter()), this, SLOT(lineHoverEntered()));
    connect(canvas, SIGNAL(lineHoverLeave()), this, SLOT(lineHoverLeft()));
    connect(canvas, SIGNAL(pointHoverEnter()), this, SLOT(pointHoverEntered()));
    connect(canvas, SIGNAL(pointHoverLeave()), this, SLOT(pointHoverLeft()));
    connect(canvas, SIGNAL(pointDragStart()), this, SLOT(pointDragStarted()));
    connect(canvas, SIGNAL(pointDragStop()), this, SLOT(pointDragStoped()));
    connect(canvas, SIGNAL(lineDragStart()), this, SLOT(lineDragStarted()));
    connect(canvas, SIGNAL(lineDragStop()), this, SLOT(lineDragStoped()));
    connect(timer, SIGNAL(timeout()), this, SLOT(nextLevel()));
}

void Playground::clicked(ButtonItem *who)
{
    if (returnBg == who) {
        emit returnClicked();
        canvas->clear();
        canvas->setNameBanner("[untitled]");
    }
}

void Playground::lineHoverEntered()
{
    emit lineHoverEnter();
}

void Playground::lineHoverLeft()
{
    emit lineHoverLeave();
}

void Playground::pointHoverEntered()
{
    emit pointHoverEnter();
}

void Playground::pointHoverLeft()
{
    emit pointHoverLeave();
}

void Playground::pointDragStarted()
{
    emit pointDragStart();
}

void Playground::pointDragStoped()
{
    if (judger->findCrossingPoint(canvas->getLinesList()).size()==0)
        doSolved();
    emit pointDragStop();
}

void Playground::lineDragStarted()
{
    emit lineDragStart();
}

void Playground::lineDragStoped()
{
    if (judger->findCrossingPoint(canvas->getLinesList()).size()==0)
        doSolved();
    emit lineDragStop();
}

void Playground::setPlayLevel(Level *level)
{
    current = level;

    if (level == NULL)
    {
        canvas->copyGraph(QLinkedList<Point*>(), QLinkedList<Line*>());
        return;
    }
    else
        canvas->copyGraph(level->getPointsList(), level->getLinesList());
    canvas->setNameBanner(level->getName());
    canvas->setState(commons::PLAYING);
}

void Playground::doSolved()
{
    canvas->setState(commons::SOLVED);
    greatImg->setVisible(true);
    timer->setInterval(2000);
    timer->setSingleShot(true);
    timer->start();
}

void Playground::nextLevel()
{
    greatImg->setVisible(false);
    canvas->clear();
    emit toNextLevel();
}

Level *Playground::getCurrentLevel()
{
    return current;
}
